﻿<?php 

	//接受消息推送请求参数
	$gameName = $_REQUEST['packageName'];//游戏名

	if(empty($date)) $date = time();	

	//定义变量
	$queryToday = '';//今日查询
	$query7Days = '';//最近七日查询
	$queryContent ='';//内容查询
	$conn = null;//数据连接
	$result = null;//结果集
	$otherArr = array();//其它游戏数组
	$gameArr=array();//发送游戏数组
	
	$login_t = $_REQUEST['time'];
	$decide = decideTime("11:31","11:31",$login_t);
	if($decide == "false")
	{
		echo "upaddr*0";
		exit;
	}
	//else
	//{
	//数据库操作
	$conn = mysql_connect('localhost','root','') or die("connect db err:".mysql_error);
	mysql_select_db('info_push') or die("select db err:".mysql_error);
	mysql_query('set names utf8');		
	
	//今天查询语句并且在title字段中不含有update字符的数据	
	$queryToday = "SELECT * FROM push WHERE to_days(time) = to_days(now()) and !FIND_IN_SET('/update/12.0.3',title)";
	//近7天查询语句
	$query7Days = "select id,gameName from push where date_sub(curdate(),interval 7 day) <=time and !FIND_IN_SET('/update/12.0.3',title)";
	
	// 查询当天
	try
	{
		$result = mysql_query($queryToday,$conn) or die("select querytoday failed:". mysql_error());
	}catch(Exception $e)
	{
		echo $e;
	}
	//若无当天记录，查询近7天记录
	try
	{
		if(mysql_num_rows($result)==0) 
		{	
			$result = mysql_query($query7Days,$conn) or die("select query7days failed:". mysql_error());
		}
	}catch(Exception $e)	
	{
		echo $e;
	}	
	
	//var_dump(is_resource($result));
	//获取结果集数组
	if(is_resource($result))
	{ 	
		while($row = mysql_fetch_assoc($result))
		{
			if($row['gameName'] !=$gameName)	//其它游戏数组	
				array_push($otherArr,$row); //array  {0 => [array 'id' => string '1' (length=1)] }
			else//发送游戏数组
				array_push($gameArr,$row);
		}
	}
	//var_dump($otherArr);
	//随机数据抽取	
	
	$count = !empty($otherArr) ? count($otherArr) : count($gameArr);
	//var_dump($otherArr);
	if ($count!=0)
	{
		$totalCount =  mt_rand(0,$count-1);
		$id = !empty($otherArr) ? $otherArr[$totalCount]['id'] : $gameArr[$totalCount]['id'];//获取内容信息id
		//var_dump(!empty($otherArr) );
		$queryContent = "select title,content from push where id={$id} and gameName <> '{$gameName}'";
	}
	else	
		$queryContent = "select title,content from push limit 1";
	
	//var_dump($gameArr[$totalCount]['id']);
	$res = mysql_query($queryContent,$conn) or die('queryContent failed');
	$row = mysql_fetch_assoc($res);
	echo $row['content'];
   // }
	
function decideTime($beginTime,$endTime,$login_time)
{
	$strb = explode(":",$beginTime);
	$stre = explode(":",$endTime);
	$strl = explode(":",$login_time);
	if(count($strb)!=2)
	{
	    return "false";
		exit;
	}
	if(count($stre)!=2)
	{
	    return "false";
		exit;
	}	
	if(count($strl)!=2)
	{
	    return "false";
		exit;
	}	
    if($strl[0]>=$strb[0] && $strl[0]<=$stre[0] && $strl[1]>=$strb[1] && $strl[1]<=$stre[1])
		return "true";
    else
		return "false";	
}
	
?>